package com.bootdo.common.aspect;

import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.bootdo.xzl.dao.BrowseRecordDao;
import com.bootdo.xzl.domain.BrowseRecordDO;

@Aspect
@Component
public class InterfaceAspect {
	
	@Autowired
	private BrowseRecordDao browseRecordDao;
	
	@Before("execution(public * com.bootdo.xzl.controller.HousesInfoController.housesInfo(..))")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        
    	// 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        	
        if(joinPoint!=null && joinPoint.getArgs()!=null&&joinPoint.getArgs().length>0){
        		
        		int id = (int) joinPoint.getArgs()[0];
        		String ip = request.getRemoteAddr();
        		
        		BrowseRecordDO browseRecord = new BrowseRecordDO();
        		browseRecord.setOfficeid(id);
        		browseRecord.setBrowseRecordIp(ip);
        		
        		browseRecordDao.save(browseRecord);     		
        }
	}
	
}
